

/** 资源包管理器 */

import { ConfigModule_blxf } from "../Module/ConfigModule";
import PublicModule_blxf from "../Module/PublicModule";
import { EnumManager_blxf } from "./EnumManager";

const {ccclass, property} = cc._decorator;

@ccclass
export class BundleManager_blxf {

    private constructor() { }

    private static _BundleManager_blxf: BundleManager_blxf = new BundleManager_blxf();

    public static get instance() {
        return this._BundleManager_blxf;
    }

    @property({ tooltip: 'Csv分包' })
    Bundle_Csv = null;

    @property({ tooltip: 'Font分包' })
    Bundle_Font = null;

    @property({ tooltip: 'Image分包' })
    Bundle_Image = null;

    @property({ tooltip: 'Spine分包' })
    Bundle_Spine = null;

    @property({ tooltip: 'Spine_Main分包' })
    Bundle_Spine_Main = null;

    @property({ tooltip: 'Spine_Game1分包' })
    Bundle_Spine_Game1 = null;

    @property({ tooltip: 'Spine_Game2分包' })
    Bundle_Spine_Game2 = null;

    @property({ tooltip: 'Spine_Room分包' })
    Bundle_Spine_Room = null;
    

    @property({ tooltip: 'Prefab分包' })
    Bundle_Prefab = null;

    // @property({ tooltip: 'Audio分包' })
    // Bundle_Audio = null;

    @property({ tooltip: 'Audio_Main分包' })
    Bundle_Audio_Main = null;

    @property({ tooltip: 'Audio_Bgm分包' })
    Bundle_Audio_Bgm = null;

    @property({ tooltip: 'Audio_Game1分包' })
    Bundle_Audio_Game1 = null;

    @property({ tooltip: 'Audio_Game2分包' })
    Bundle_Audio_Game2 = null;


    /** 本次加载分包数 */
    _loadBundleNum = 0;
    /** 已加载分包列表 */
    _loadBundleList = [];
    /** 本次加载分包包名数组 */
    _bundleNames = [];

    /**
     * 加载游戏Bundle
     */
    loadGameBundle_blxf ( cb, bundleId: number = 0 ) {
        let _bundleTypes: number[] = this.onBundleTypes_blxf( bundleId );
        this.loadBundleByType_blxf( _bundleTypes, cb );
    };

    // 获取bundle类型
    onBundleTypes_blxf ( bundleId: number ) {
        let _bundleTypes = [ bundleId ];

        if ( bundleId == 0 && ConfigModule_blxf.gameType == 1 ) {
            _bundleTypes.push( 1 );

            PublicModule_blxf.addBundleType( 1 );
            PublicModule_blxf.addBundleType( 3 );
            PublicModule_blxf.addBundleIds( [ 3 ].concat(_bundleTypes) );
        }

        if ( bundleId == 0 && ConfigModule_blxf.gameType == 5 ) {
            _bundleTypes.push( 2 );

            PublicModule_blxf.addBundleType( 2 );
            PublicModule_blxf.addBundleIds( _bundleTypes );
        }

        return _bundleTypes;
    }

    /**
     * 动态加载子包
     * @param bundleTypes [ bundleType: 0：基础包、1：基础音效包、其它：乐手歌曲分名 ]
     * @param cb 返回加载分包进度
     */
    loadBundleByType_blxf ( bundleTypes, cb ) {
        let _gameBundleType = EnumManager_blxf.instance.getGameBundleType();

        bundleTypes.forEach( bundleType => {
            let _bundleNames = [];
            switch ( bundleType ) {
                case 1 :
                    _bundleNames = [
                        _gameBundleType.BUNDLE_SPINE_GAME1,
                        _gameBundleType.BUNDLE_AUDIO_GAME1,
                        _gameBundleType.BUNDLE_AUDIO_BGM,
                    ]; break;

                case 3 :
                    _bundleNames = [
                        _gameBundleType.BUNDLE_SPINE_ROOM
                    ]; break;

                default : 
                    _bundleNames = [
                        _gameBundleType.BUNDLE_CSV,
                        // _gameBundleType.BUNDLE_FONT,
                        _gameBundleType.BUNDLE_IMAGE,

                        // _gameBundleType.BUNDLE_SPINE,
                        _gameBundleType.BUNDLE_SPINE_MAIN,
                        // _gameBundleType.BUNDLE_SPINE_GAME1,
                        // _gameBundleType.BUNDLE_SPINE_GAME2,

                        _gameBundleType.BUNDLE_PREFAB,

                        // _gameBundleType.BUNDLE_AUDIO,
                        _gameBundleType.BUNDLE_AUDIO_MAIN
                        // _gameBundleType.BUNDLE_AUDIO_BGM,
                        // _gameBundleType.BUNDLE_AUDIO_GAME1,
                        // _gameBundleType.BUNDLE_AUDIO_GAME2
                    ]; break;
            }

            _bundleNames.forEach( bundleName =>{
                let _isCheck = this._bundleNames.includes( bundleName );
                !_isCheck && this._bundleNames.push( bundleName );
            })
        })
        cc.log('loadManagerBundle >>', bundleTypes, this._bundleNames);

        this.loadBundle_blxf( cb );
    }

    /**
     * 加载子包
     * @param cb 
     * @returns 
     */
    loadBundle_blxf ( cb ) {
        let _bundleName = this._bundleNames[this._loadBundleNum];
        cc.error('_bundleName >>', _bundleName);
        if ( !_bundleName ) return cb && cb( 1 );

        let t = Date.now();
        cc.log(`${_bundleName}子包加载开始！`, _bundleName);
        cc.assetManager.loadBundle(_bundleName, (err) =>{
            if (err) return console.error(`loadBundle <${_bundleName}> erroro: ${err}`);
            console.log(`${_bundleName}子包加载完成！`, (Date.now() - t) / 1000);

            // 记录分包
            if ( _bundleName ) {
                this[`Bundle_${_bundleName}`] = cc.assetManager.getBundle(_bundleName);
            }

            let _bundleLen = this._bundleNames.length;

            // 累计加载数
            this._loadBundleNum += 1;
            this._loadBundleList.push(_bundleName);

            // 加载进度
            let _loadRatio = this._loadBundleNum / _bundleLen;
            cb && cb( _loadRatio, _bundleName );

            // 全部子包加载完成
            let _isAllOk = this._loadBundleNum == _bundleLen;
            if ( _isAllOk ) return;

            // 递归加载下一个子包
            this.loadBundle_blxf( cb );
        });
    }
}
